from flask import request
from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.shop_fields_def import shop_fields,shop_pagnation_fields,shop_approval_fields
from services.online_mall.shop_service import ShopService


class ShopNameListApi(Resource):
    @login_required
    @marshal_with(shop_pagnation_fields)
    @check_perms(perms=['mall:shop:list'])
    def get(self):
        """
        获取店铺列表
        :return:
        """
        return ShopService.get_shop_name_list(current_user.current_tenant_id)

class ShopManageApi(Resource):
    @login_required
    @marshal_with(shop_pagnation_fields)
    @check_perms(perms=['mall:shop:list'])
    def get(self):
        """
        获取店铺列表
        :return:
        """
        parser = reqparse.RequestParser()
        parser.add_argument('shop_type',type=int,required=False,location='args')
        parser.add_argument('industry',type=int,required=False,location='args')
        parser.add_argument('mobile',type=str,required=False,location='args')
        parser.add_argument('service_tel',type=str,required=False,location='args')
        parser.add_argument('province',type=str,required=False,location='args')
        parser.add_argument('city',type=str,required=False,location='args')
        parser.add_argument('district',type=str,required=False,location='args')
        parser.add_argument('zip_code',type=str,required=False,location='args')
        parser.add_argument('shop_status',type=str,required=False,location='args')
        parser.add_argument('transport_type',type=StrBoolNone,required=False,location='args')
        parser.add_argument('is_distribution',type=StrBoolNone,required=False,location='args')
        parser.add_argument('page', type=int, required=True, default=1,location='args')
        parser.add_argument('page_size', type=int, required=True, default=10,location='args')
        args = parser.parse_args()
        return ShopService.get_shop_list(current_user.current_tenant_id,args)
    
    @login_required
    @marshal_with(shop_fields)
    @check_perms(perms=['mall:shop:create'])
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('name',type=str,required=True,location='json')
        parser.add_argument('master_id',type=str,required=True,location='json')
        parser.add_argument('shop_type',type=int,required=False,location='json')
        parser.add_argument('intro',type=str,required=False,location='json')
        parser.add_argument('notice',type=str,required=False,location='json')
        parser.add_argument('industry',type=int,required=False,location='json')
        parser.add_argument('mobile',type=str,required=False,location='json')
        parser.add_argument('service_tel',type=str,required=False,location='json')
        parser.add_argument('lat',type=float,required=False,location='json')
        parser.add_argument('lng',type=float,required=False,location='json')
        parser.add_argument('address',type=str,required=False,location='json')
        parser.add_argument('province',type=str,required=False,location='json')
        parser.add_argument('city',type=str,required=False,location='json')
        parser.add_argument('district',type=str,required=False,location='json')
        parser.add_argument('zip_code',type=str,required=False,location='json')
        parser.add_argument('shop_logo_file_id',type=str,required=False,location='json')
        parser.add_argument('ShopPhotos',type=str,action='append',required=False,location='json')
        parser.add_argument('open_time',type=str,required=False,location='json')
        parser.add_argument('shop_status',type=str,required=False,location='json')
        parser.add_argument('transport_type',type=StrBoolNone,required=False,location='json')
        parser.add_argument('fixed_freight',type=int,required=False,location='json')
        parser.add_argument('full_free_shipping',type=int,required=False,location='json')
        parser.add_argument('is_distribution',type=StrBoolNone,required=False,location='json')
        args = parser.parse_args()
        return ShopService.create_shop(current_user,args)
    
    @login_required
    @marshal_with(shop_fields)
    @check_perms(perms=['mall:shop:update'])
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id',type=str,required=True,location='json')
        parser.add_argument('name',type=str,required=True,location='json')
        parser.add_argument('master_id',type=str,required=True,location='json')
        parser.add_argument('shop_type',type=int,required=False,location='json')
        parser.add_argument('intro',type=str,required=False,location='json')
        parser.add_argument('notice',type=str,required=False,location='json')
        parser.add_argument('industry',type=int,required=False,location='json')
        parser.add_argument('mobile',type=str,required=False,location='json')
        parser.add_argument('service_tel',type=str,required=False,location='json')
        parser.add_argument('lat',type=float,required=False,location='json')
        parser.add_argument('lng',type=float,required=False,location='json')
        parser.add_argument('address',type=str,required=False,location='json')
        parser.add_argument('province',type=str,required=False,location='json')
        parser.add_argument('city',type=str,required=False,location='json')
        parser.add_argument('district',type=str,required=False,location='json')
        parser.add_argument('zip_code',type=str,required=False,location='json')
        parser.add_argument('shop_logo_file_id',type=str,required=False,location='json')
        parser.add_argument('ShopPhotos',type=str,action='append',required=False,location='json')
        parser.add_argument('open_time',type=str,required=False,location='json')
        parser.add_argument('shop_status',type=str,required=False,location='json')
        parser.add_argument('transport_type',type=StrBoolNone,required=False,location='json')
        parser.add_argument('fixed_freight',type=int,required=False,location='json')
        parser.add_argument('full_free_shipping',type=int,required=False,location='json')
        parser.add_argument('is_distribution',type=StrBoolNone,required=False,location='json')
        args = parser.parse_args()
        return ShopService.update_shop(current_user,args)
    
    @login_required
    @check_perms(perms=['mall:shop:del'])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id',type=str,required=True,location='json')
        args = parser.parse_args()
        return ShopService.delete_shop(current_user,args)

    
class ShopInfoApi(Resource):
    @login_required
    @check_perms(perms=['mall:shop:info'])
    @marshal_with(shop_fields)
    def get(self,shop_id:str):
        return ShopService.get_shop_info(shop_id)
    
class MyShopInfoApi(Resource):
    @login_required
    @check_perms(perms=['mall:shop:info'])
    @marshal_with(shop_pagnation_fields)
    def get(self):
        return ShopService.get_my_shop_info(current_user)

class ShopDistributionApi(Resource):
    @login_required
    @check_perms(perms=['mall:shop:update'])
    @marshal_with(shop_fields)
    def put(self,shop_id:str):
        is_distribution = request.args.get("is_distribution",False)
        args = {
            "id":shop_id,
            "is_distribution":is_distribution
        }
        return ShopService.update_shop(current_user,args)


class ShopStatusApi(Resource):
    @login_required
    @check_perms(perms=['mall:shop:update'])
    @marshal_with(shop_fields)
    def put(self,shop_id:str):
        shop_status = request.args.get("shop_status",False)
        args = {
            "id":shop_id,
            "shop_status":shop_status
        }
        return ShopService.update_shop(current_user,args)

class ShopApprovalApi(Resource):
    @login_required
    @check_perms(perms=['mall:shop_approval:list'])
    @marshal_with(shop_approval_fields)
    def get(self,shop_id:str):
        return ShopService.approval_list(shop_id)
    
    @login_required
    @check_perms(perms=['mall:shop_approval:approval'])
    @marshal_with(shop_approval_fields)
    def put(self,shop_id:str):
        parser = reqparse.RequestParser()
        parser.add_argument('approval',type=StrBoolNone,required=True,location='json')
        parser.add_argument('content',type=str,required=True,location='json')
        args = parser.parse_args()
        approvaled = args.get("approval")
        content = args.get('content','')
        return ShopService.approval(current_user,shop_id,content,approvaled)
    

api.add_resource(ShopApprovalApi,'/shop/<string:shop_id>/approval')
api.add_resource(ShopNameListApi,'/shop/shopDetail/namelist',endpoint='shopDetail/namelist')
api.add_resource(ShopStatusApi,'/shop/shopDetail/<string:shop_id>/status',endpoint='shopDetail/status')
api.add_resource(ShopDistributionApi,'/shop/shopDetail/<string:shop_id>/distribution',endpoint='shopDetail/distribution')
api.add_resource(MyShopInfoApi,'/shop/shopDetail/info',endpoint='shopDetail/my')
api.add_resource(ShopInfoApi,'/shop/shopDetail/info/<string:shop_id>',endpoint='shopDetail/info')
api.add_resource(ShopManageApi,'/shop/shopDetail',endpoint='shopDetail')